package cn.edu.cjw.APMFP.ui.panel;

import cn.edu.cjw.APMFP.BOMTable.Service.BTService;
import cn.edu.cjw.APMFP.BOMTable.Service.BTServiceImpl;
import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.Resource.Resource;
import cn.edu.cjw.APMFP.Resource.Service.ResourceService;
import cn.edu.cjw.APMFP.Resource.Service.ResourceServiceImpl;
import cn.edu.cjw.APMFP.ui.CenterPanel;
import cn.edu.cjw.APMFP.ui.dialog.Bom1Dialog;
import cn.edu.cjw.APMFP.ui.table.Bom2TableModel;
import cn.edu.cjw.APMFP.ui.table.BomTableModel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @package cn.edu.cjw.APMFP.ui.panel
 * @className Bom1Panel
 * @note 成品
 * @author wind
 * @date 2022/3/12 19:28
 */
public class Bom1Panel extends JPanel {

    private JButton inBtn = new JButton("成品入库");

    private JButton outBtn = new JButton("成品出库");

//    private BTService service = new BTServiceImpl();

    private ResourceService service = new ResourceServiceImpl();

    private JPanel northPanel = new JPanel();

    private JScrollPane centerPanel = new JScrollPane();

    private JTable table = new JTable();

    private List<Resource> list = new ArrayList<>();

    private CenterPanel mainPanel;

    public Bom1Panel(CenterPanel mainPanel){
        this.mainPanel = mainPanel;
        init();
    }

    private void init(){
        this.setLayout(new BorderLayout());


        northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        northPanel.add(inBtn);
        northPanel.add(outBtn);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getTableHeader().setReorderingAllowed(false);
        initTable();
        centerPanel.setViewportView(table);


        inBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                int row = table.getSelectedRow();
////                if(row == -1){
////                    JOptionPane.showMessageDialog(null, "请选择修改行");
////                    return;
////                }
                new Bom1Dialog(null, null);
            }
        });

        outBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                if(row == -1){
                    JOptionPane.showMessageDialog(null, "请选择删除行");
                    return;
                }
                new Bom1Dialog(list.get(row), null);
            }
        });

        this.add(northPanel, BorderLayout.NORTH);
        this.add(centerPanel, BorderLayout.CENTER);
    }

    public void initTable(){
        this.list.clear();
        try {
            this.list.addAll(service.showProduct());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Bom2TableModel model = new Bom2TableModel(list);
        table.setModel(model);
        model.fireTableDataChanged();
    }
}
